<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Map extends CI_Controller
{
	private $_food;
	private $_wood;
	private $_metal;
	private $_petroleum;
	function __construct()
	{
		parent::__construct();
		$this->_food = $this->config->item('food');
		$this->_wood = $this->config->item('wood');
		$this->_metal = $this->config->item('metal');
		$this->_petroleum = $this->config->item('petroleum');
		$this->load->library('Mydatetime');
		$this->load->library('session');
		$this->load->model('map/map_model');
	}
	function test()
    {
        $data = array();
        $userid = $this->session->userdata('uid');
        $user_oasis_orderid = $this->session->userdata('oasis_orderid');
        $locationId = $this->map_model->ww_get_map_user_info($userid, $user_oasis_orderid);
        $x = $locationId['x']!='' ? (int)$locationId['x'] : 0;
        $y = $locationId['y']!='' ? (int)$locationId['y'] : 0;
        $info = $this->map_model->ww_get_map_by_user_np($x, $y, 400);
        $arrx = $this->mycommon->createRangeRule($x, 400);
        $arry = $this->mycommon->createRangeRule($y, 400);
        
        $data['info'] = $info;
        $data['arrx'] = $arrx;
        $data['arry'] = $arry;
		// header
		
		// content
		$this->load->view('map/demo', $data);
    }
	public function index()
	{
        if(!$this->session->userdata('username'))
        {
            redirect(site_url('login'));
            die();
        }
        $data = array();
        $userid = $this->session->userdata('uid');
        $user_oasis_orderid = $this->session->userdata('oasis_orderid');
        $locationId = $this->map_model->ww_get_map_user_info($userid, $user_oasis_orderid);
        $x = $locationId['x']!='' ? (int)$locationId['x'] : 0;
        $y = $locationId['y']!='' ? (int)$locationId['y'] : 0;
        $info = $this->map_model->ww_get_map_by_user_np($x, $y, 400);
        $arrx = $this->mycommon->createRangeRule($x, 400);
        $arry = $this->mycommon->createRangeRule($y, 400);
        
        $data['info'] = $info;
        $data['arrx'] = $arrx;
        $data['arry'] = $arry;
		// header
		
		// content
		$this->load->view('map/mappreview_view', $data);
		
		// footer
	}
    function review()
    {
        $data = array();
        $userid = $this->session->userdata('uid');
        $user_oasis_orderid = $this->session->userdata('oasis_orderid');
        $loc_user = $this->map_model->ww_get_map_user_info($userid, $user_oasis_orderid);
        $u_arrxy =  array(
                            'x' => $loc_user['x'],
                            'y' => $loc_user['y']
                        );
        
        $mapid = $this->input->get('mapid', true);
        $loc_target = $this->map_model->ww_get_info_map_by_user($mapid);
        $t_arrxy =  array(
                            'x' => $loc_target['x'],
                            'y' => $loc_target['y']
                    );
        
        $distanceTime = $this->mycommon->distanceTime($u_arrxy, $t_arrxy, 5, 400);
        $x = $y = 388;
        $info = $this->map_model->ww_get_map_by_user_np($x, $y, 400);
        $data['info'] = $info;
		// header
		
		// content
		$this->load->view('map/map_user', $data);
		
		// footer
    }
    function gmap()
    {
        $typeview = (int)$this->input->post('typeview');
        $xx = (int)$this->input->post('xx');
        $yy = (int)$this->input->post('yy');
        $rx = (int)$this->input->post('rx');
        $ry = (int)$this->input->post('ry');
        
        if($typeview == 2 || $typeview == 3)
        {
            $info = $this->map_model->ww_get_map_by_user($rx, $ry, 400, $typeview);
        }
        else if($typeview == 1)
        {
            $info = $this->map_model->ww_get_map_by_user($xx, $ry, 400, $typeview);
        }
        else if($typeview == 4)
        {
            $info = $this->map_model->ww_get_map_by_user($rx, $yy, 400, $typeview);
        }
        else if($typeview == 0)
        {
            $info = $this->map_model->ww_get_map_by_user_np($xx, $yy, 400);
        }
        else if($xx>400 || $xx<-400 ||$yy>400 || $yy<-400)
        {
            echo 'error';
            die();
        }
        $i = 0;
        $onelstField = array();
        $allLstField = array();
        foreach($info as $if)
        {
            if($i == 13)
            {
                $allLstField[] = join('","', $onelstField);
                $onelstField = null;
                $i = 0;
            }
            $onelstField[] = $if['image'].','.site_url('map').'/review?mapid='.$if['id'].','.$if['x'].','.$if['y'];
            $i++;
        }
        $allLstField[] = join('","', $onelstField);
        $data['lst'] = $allLstField;
        echo json_encode($data['lst']);
    }
}